#include <iostream>
#include "maxSubArray.h"
#include <cstdlib>

using namespace std;

bool maxSubArray(int *array, int elem_num, int &max_sum, int &start, int &end) {
    if (array == NULL ||
        elem_num <= 0) {
        cout << "Invalid arguments!" << endl;
        return false;
    }

    start = end = 0;
    int start_tmp = 0;
    int max_sum_cur = array[0];
    int max_sum_all = array[0];
    int i;

    for (i = 1; i < elem_num; i++) {
        max_sum_cur += array[i];
        if (max_sum_cur < array[i]) {
            max_sum_cur = array[i];
            start_tmp = i;
        }

        if (max_sum_all < max_sum_cur) {
            max_sum_all = max_sum_cur;
            start = start_tmp;
            end = i;
        }
    }

    max_sum = max_sum_all;

    return true;
}


